/*
 * Scaffold Application Framework
 *
 * Copyright (c) 2010 Sii Sp. z o.o. All rights reserved.
 */
package com.rstrulak.constant;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Carries information about the sorting that should be applied to a requested
 * results. It handles infinite number of sorting fields with separate order for
 * each one.
 * 
 * @author Jakub Ciechan
 * @version 1.0
 */
public class SortInfo {
    /**
     * List of fields that the sorting should be applied to in the order that
     * they are in the list.
     */
    private List<SortField> fields = new ArrayList<SortField>();

    /**
     * Default constructor.
     */
    public SortInfo() {
    }

    /**
     * Adds a new sort field with a specified order.
     * 
     * @param fieldName - field name to apply sorting by
     * @param order - order to apply to the chosen field
     */
    public void addSortField(String fieldName, SortOrder order) {
        fields.add(new SortField(fieldName, order));
    }

    /**
     * Convenience method that adds a new sort field with an ascending order.
     * 
     * @param fieldName - field name to apply sorting by
     */
    public void addSortFieldAscending(String fieldName) {
        fields.add(new SortField(fieldName, SortOrder.ASCENDING));
    }

    /**
     * Convenience method that adds a new sort field with a descending order.
     * 
     * @param fieldName - field name to apply sorting by
     */
    public void addSortFieldDescending(String fieldName) {
        fields.add(new SortField(fieldName, SortOrder.DESCENDING));
    }

    /**
     * Getter for fields.
     * 
     * @return the fields
     */
    public List<SortField> getFields() {
        return fields;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("fields", fields).toString();
    }

    /** Sort orders. */
    public static enum SortOrder {
        /** Ascending order. */
        ASCENDING,
        /** Descending order. */
        DESCENDING;
    }
}
